<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ include file="/WEB-INF/taglib.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>定时任务</title>
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
    <script type="text/javascript">
        var path = "${path}";
        var params = new Object();
        var cronCron ="";

        var cronBds = "";
        var bj = "1";
        var url = path + "/system/cronjob/list";
        $(function () {
            //设置AjaxLoading
            setAjaxLoading();
            var grid = $("#tt");
            initGrid(grid);
        });
        //初始化表格和数据
        function initGrid(grid) {
            var columns = [
                {field: "jobId", title: "ID", hidden: true, align: "left", sortable: false, resizable: true, width: 80},
                {
                    field: "jobName",
                    title: "任务名称",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 130
                },
                {
                    field: "jobGroup",
                    title: "任务分组",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 120
                },
                {
                    field: "validMark",
                    title: "任务状态",
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 80,
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "启动";
                        } else if (value == 0) {
                            return "停止";
                        }
                    }
                },
                {
                    field: "cron",
                    title: "表达式",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 80
                },
                {
                    field: "remark",
                    title: "描述",
                    align: "left",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 180
                },
                {
                    field: "isConcurrent",
                    title: "任务是否串行",
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 120,
                    formatter: function (value, row, index) {
                        if (value == 0) {
                            return "是";
                        } else if (value == 1) {
                            return "否";
                        }
                    }
                },
                {
                    field: "beanId",
                    title: "BEAN_ID",
                    hidden: true,
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 200
                },

                {
                    field: "methodCall",
                    title: "方法名",
                    hidden: true,
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 80
                }
                ,
                {
                    field: "timeSet",
                    title: "时间设置",
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 100
                    ,formatter: function (value, row, index) {
                        if (value == '01') {
                            return "一般设置";
                        } else if (value == '02') {
                            return "服务调度";
                        }else if(value == '03'){
                            return "存储过程调度";
                        }
                    }
                }
                ,
                {
                    field: "storedName",
                    title: "存储过程名称",
                    hidden: true,
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 80
                }

                ,
                {
                    field: "timeEffect",
                    title: "生效时间",
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 200,
                    formatter: function (value) {
                        if (value == null) {
                            return;
                        }
                        var date = new Date();
                        date.setTime(value);
                        var y = date.getFullYear();
                        var M = date.getMonth() + 1;
                        var d = date.getDate();
                        var h = date.getHours();
                        var m = date.getMinutes();
                        var s = date.getSeconds();
                        if (M < 10) {
                            M = "0" + M;
                        }
                        if (d < 10) {
                            d = "0" + d;
                        }
                        if (h < 10) {
                            h = "0" + h;
                        }
                        if (m < 10) {
                            m = "0" + m;
                        }
                        if (s < 10) {
                            s = "0" + s;
                        }
                        return y + '-' + M + '-' + d + " " + h + ":" + m + ":" + s;
                    }
                }
                ,
                {
                    field: "timeLose",
                    title: "失效时间",
                    align: "center",
                    halign: "center",
                    sortable: false,
                    resizable: true,
                    width: 200,
                    formatter: function (value) {
                        if (value == null) {
                            return;
                        }
                        var date = new Date();
                        date.setTime(value);
                        var y = date.getFullYear();
                        var M = date.getMonth() + 1;
                        var d = date.getDate();
                        var h = date.getHours();
                        var m = date.getMinutes();
                        var s = date.getSeconds();
                        if (M < 10) {
                            M = "0" + M;
                        }
                        if (d < 10) {
                            d = "0" + d;
                        }
                        if (h < 10) {
                            h = "0" + h;
                        }
                        if (m < 10) {
                            m = "0" + m;
                        }
                        if (s < 10) {
                            s = "0" + s;
                        }
                        return y + '-' + M + '-' + d + " " + h + ":" + m + ":" + s;
                    }
                }

//                ,{
//                    field: "beanClass",
//                    title: "任务调用的方法名",
//                    align: "center",
//                    halign: "center",
//                    sortable: false,
//                    resizable: true,
//                    width: 150
//                }
//                ,{
//                    field: 'OPERATE', title: '操作', width: 160, align: "center", formatter: function(val,row,index){
//                    var btn = '<a class="editcls" onclick="puaseRow('+index+')" href="javascript:void(0)">暂停</a>' + '&nbsp&nbsp&nbsp'+
//                        '<a class="editcls" onclick="recRow('+index+')" href="javascript:void(0)">恢复</a>'+ '&nbsp&nbsp&nbsp&nbsp'+
//                        '<a class="editcls" onclick="rightnowRow('+index+')" href="javascript:void(0)">立即执行</a>';
//                    return btn;
//                    }
//                }
            ];
            initPageGrid(grid, params, url, columns, "#tb", true);
            var gridOptions = $("#tt").datagrid("options");
            gridOptions.onLoadSuccess = function (data) {
                if (data.rows.length > 0) {
                    grid.datagrid('selectRow', 0);
                    grid.datagrid("fixRownumber");
                }
            }
            gridOptions.onDblClickRow = function (index, row) {
                btnUpdate();
            }
            btnQuery();
        }

        //暂停操作按钮
        function puaseRow(index) {
            $("#tt").datagrid('selectRow',index);// 关键在这里
            var row = $("#tt").datagrid("getSelected");
            var param = new Object();
            param.jobId = row.jobId;
            param.jobName = row.jobName;
            param.jobGroup = row.jobGroup;
            $.messager.alert("问题", "请确认是否要暂停？","confirm", function () {

                    $.ajax({
                    type: "POST",
                    url: path + "/system/cronjob/puaseRow",
                    dataType: "json",
                    data: param,
                    async: false,

                    success: function callback(data) {
                        var analyMsg = data;
                        if (analyMsg.code != '0000') {
                            $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                            });
                        } else {
                            $.messager.alert("提醒", "暂停定时任务成功！", "info", function () {
                                btnQuery();
                                setTimeout("closeLayer()", 100);//设置延时，防止IE下出现异常
                            });
                        }
                    },
                    error: function (data, textstatus) {
                        $.messager.alert('操作提示', data.responseText, 'info', function () {
                        });
                    }
                });
            });
        }
        //恢复
        function recRow(index) {
            $("#tt").datagrid('selectRow',index);// 关键在这里
            var row = $("#tt").datagrid("getSelected");
            var param = new Object();
            param.jobId = row.jobId;
            param.jobName = row.jobName;
            param.jobGroup = row.jobGroup;
            $.messager.alert("问题", "请确认是否要恢复？","confirm", function () {
                    $.ajax({
                    type: "POST",
                    url: path + "/system/cronjob/recRow",
                    dataType: "json",
                    data: param,
                    async: false,

                    success: function callback(data) {
                        var analyMsg = data;
                        if (analyMsg.code != '0000') {
                            $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                            });
                        } else {
                            $.messager.alert("提醒", "恢复定时任务成功！", "info", function () {
                                btnQuery();
                                setTimeout("closeLayer()", 100);//设置延时，防止IE下出现异常
                            });
                        }
                    },
                    error: function (data, textstatus) {
                        $.messager.alert('操作提示', data.responseText, 'info', function () {
                        });
                    }
                });
            });

        }
        //立即执行
        function rightnowRow(index) {
            $("#tt").datagrid('selectRow',index);// 关键在这里
            var row = $("#tt").datagrid("getSelected");
            var param = new Object();
            param.jobId = row.jobId;
            param.jobName = row.jobName;
            param.jobGroup = row.jobGroup;
            $.messager.alert("问题", "请确认是否要立即执行？","confirm",function () {

                    $.ajax({
                    type: "POST",
                    url: path + "/system/cronjob/rightnowRow",
                    dataType: "json",
                    data: param,
                    async: false,

                    success: function callback(data) {
                        var analyMsg = data;
                        if (analyMsg.code != '0000') {
                            $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                            });
                        } else {
                            $.messager.alert("提醒", "立即执行定时任务成功！", "info", function () {
                                btnQuery();
                                setTimeout("closeLayer()", 100);//设置延时，防止IE下出现异常
                            });
                        }
                    },
                    error: function (data, textstatus) {
                        $.messager.alert('操作提示', data.responseText, 'info', function () {
                        });
                    }
                });
            });

        }


        //查询
        function btnQuery() {
            var grid = $("#tt");
            getPage(grid, params, url);
        }
        //新增
        function btnAdd() {


            layer.open({
                type: 2,
                title: ['新增', 'background: #38acff;'],
                shadeClose: false,
                shade: [0.8, '#393D49'],
                area: ['860px', '450px'],
                end: function () {
                },
                content: '${path}/system/cronjob/toAdd'
            });
        }
        //手工补录
        function btnHandSet() {
            var row = $("#tt").datagrid("getSelected");
            if (row == null) {
                $.messager.alert("操作提示", "请选择一个记录信息！", "warning");
                return;
            }
            layer.open({
                type: 2,
                title: '手工补录',
                shadeClose: false,
                shade: 0.6,
                area: ['860px', '350px'],
                end: function () {

                },
                content: '${path}/system/cronjob/toManual'    //iframe的url
            });
        }



        //修改
        function btnUpdate() {
            var row = $("#tt").datagrid("getSelected");
            if (row == null) {
                $.messager.alert("操作提示", "请选择一个记录信息！", "warning");
                return;
            }
            layer.open({
                type: 2,
                title: ['修改', 'background: #38acff;'],
                shadeClose: false,
                shade: [0.8, '#393D49'],
                area: ['860px', '450px'],
                end: function () {

                },
                content: '${path}/system/cronjob/toUpdate'    //iframe的url
            });
        }

        //更新cron
        function btnUpdateCron() {
            var row = $("#tt").datagrid("getSelected");
            if (row == null) {
                $.messager.alert("操作提示", "请选择一个记录信息！", "warning");
                return;
            }
            layer.open({
                type: 2,
                title: ['更新Cron', 'background: #38acff;'],
                shadeClose: false,
                shade: 0.6,
                area: ['860px', '510px'],
                end: function () {

                },
                content: '${path}/system/cronjob/toUpdateCron?bj=' + bj    //iframe的url
            });
        }

        //计划任务参数设置
        function btnSetCs() {
            var row = $("#tt").datagrid("getSelected");
            if (row == null) {
                $.messager.alert("操作提示", "请选择一个记录信息！", "warning");
                return;
            }
            layer.open({
                type: 2,
                title: ['计划任务参数设置', 'background: #38acff;'],
                shadeClose: false,
                shade: 0.6,
                area: ['650px', '300px'],
                end: function () {

                },
                content: '${path}/system/cronjob/toParamIndex'    //iframe的url
            });
        }

        //删除
        function btnDel() {
            var row = $("#tt").datagrid("getSelected");
            if (row == null) {
                $.messager.alert("操作提示", "请选择一个记录信息！", "warning");
                return;
            }
            var param = new Object();
            param.jobId = row.jobId;
            param.jobName = row.jobName;
            param.jobGroup = row.jobGroup;
            $.messager.alert("问题", "请确认是否要删除？","confirm", function () {

                    $.ajax({
                        type: "POST",
                        url: path + "/system/cronjob/delete",
                        dataType: "json",
                        data: param,
                        async: false,

                        success: function callback(data) {
                            var analyMsg = data;
                            if (analyMsg.code != '0000') {
                                $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                                });
                            } else {
                                $.messager.alert("提醒", "删除定时任务成功！", "info", function () {
                                    btnQuery();
                                    setTimeout("closeLayer()", 100);//设置延时，防止IE下出现异常
                                });
                            }
                        },
                        error: function (data, textstatus) {
                            $.messager.alert('操作提示', data.responseText, 'info', function () {
                            });
                        }
                    });
            });
        }

        /**
         *
         * 时间戳转换为日期格式
         * @param nS
         * @returns {string}
         */
        function myformattery(nS) {
            var date = new Date(nS);
            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();
            var h = date.getHours();
            var mm = date.getMinutes();
            var s = date.getSeconds();
            return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d) + ' '+ h +':'+mm +':'+s;
        }

        //状态 启动/止
        function btnSta() {
            var rows = $('#tt').datagrid('getSelected');
            if (rows == null) {
                $.messager.alert("操作提示", "请选择一条信息！", "info");
                return;
            }
            var param = new Object();
            param.jobId = rows.jobId;
            param.jobName = rows.jobName;
            param.jobGroup = rows.jobGroup;
            if((rows.timeEffect)!= null){
                param.TimeEffectStr = myformattery(rows.timeEffect);
            }
            if((rows.timeLose)!= null){
                param.TimeLoseStr = myformattery(rows.timeLose);
            }

            param.timeSet= rows.timeSet;
            param.beanId = rows.beanId;
            param.methodCall = rows.methodCall;
            if(rows.timeSet=='03'){
                param.storedName=rows.storedName;
                param.dataSource=rows.dataSource;
            }
            param.cron = rows.cron;
            param.isConcurrent = rows.isConcurrent;
           // param.beanClass = rows.beanClass;
            param.validMark = rows.validMark;
            var mess = "";
            if (param.validMark == 1) {
                mess = "是否停止任务？";
            } else {
                mess = "是否启动任务？";
            }
            $.messager.alert("问题", mess,"confirm", function () {

                    $.ajax({
                        type: "POST",
                        url: path + '/system/cronjob/changeJobStatus',
                        data: param,
                        dataType: "json",
                        async: false,
                        success: function callback(data) {
                            var analyMsg = data;
                            if (analyMsg.code != "0000") {
                                $.messager.alert('操作提示', analyMsg.msg, 'info', function () {
                                });
                            } else {
                                $.messager.alert('操作提示', '任务停/启成功！', 'info', function () {
                                });
                                btnQuery();
                            }
                        }
                    });

            });
        }
    </script>
</head>

<body style="margin:0px; padding:0px;">
<div class="easyui-layout" fit="true">
    <div region="center" style="padding: 5px;" border="false">
        <div class="easyui-layout" fit="true" border="false">
            <div region="center" title="" border="false" noheader="true" style="padding: 5px 0 0 0;">
                <div class="easyui-layout" fit="true" border="false">
                    <div region="center" title="计划任务信息" border="true">
                        <table id="tt" title="" border="false"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="tb" style="padding: 1px; height: auto; display: none;">
    <div>
        <a id="btn_addNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="false"
           onclick="btnAdd()">新增</a>
        <a id="btn_editNode_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="false"
           onclick="btnUpdate()">修改</a>
        <a id="btn_remove_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove"
           plain="false" onclick="btnDel()">删除</a>
        <a id="btn_state_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-tip"
           plain="false" onclick="btnSta()">状态 启动/停止</a>
        <a id="btn_update_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit"
           plain="false" onclick="btnUpdateCron()">更新cron</a>
        <a id="btn_set_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit"
           plain="false" onclick="btnSetCs()">参数设置</a>
        <a id="btn_set_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit"
           plain="false" onclick="btnHandSet()">手工补录</a>
        <%--<a id="btn_set_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit"--%>
           <%--plain="false" onclick="btnSetCs()">参数设置</a>--%>
        <%--<a id="btn_set_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit"--%>
           <%--plain="false" onclick="btnSetCs()">参数设置</a>--%>
        <%--<a id="btn_set_tb" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit"--%>
           <%--plain="false" onclick="btnSetCs()">参数设置</a>--%>
    </div>
</div>

</body>
</html>
